<?php

namespace App\Exports;

use App\Models\OrderChild;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Concerns\FromCollection;

use Maatwebsite\Excel\Concerns\WithStyles;
//单元格格式化,单元格处理文本、数字、日期、金额等格式
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
//自动适应单元格宽
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class StockUpExport implements FromCollection, WithColumnFormatting, ShouldAutoSize,WithStyles
{
    /**导出**/
    private $params;
    /**
     * @var string[] 导出标题集合
     */
    private $headingList = [
        'id'=>'ID',
        'order_number'=>'订单编号',
        'serial_number'=>'订单流水号',
        'product_name'=>'产品名称',
        'model_number'=>'型号',
        'specification'=>'规格',
        'remark'=>'备注',
    ];



    public function __construct($params) {
        ob_end_clean();
        $this->params = $params;
    }




    /**
     * Notes: 处理导出数据表头
     * User: q
     * DateTime: 2021/10/13 18:10
     * @return array
     */
    public function headings(): array{

        return  [
            "ID",
            "订单编号",
            "订单流水号",
            "产品名称",
            "型号",
            "规格",
            "备注",
        ];
    }


    public function array() : array{
        $data[] = $this->headingList;
        $params = $this->params;
        $employee_user_id = $params['employee_user_id'];
        $array = OrderChild::where('employee_user_id',$employee_user_id)
            ->with(['order'=>function($q){
                $q->with(['color'=>function($q){
                    return $q->select('id','color');
                },'product'=>function($q){
                    return $q->select('id','product_name');
                },'customer'=>function($q){
                    return $q->select('id','customer_name','customer_address');
                },'modelNumber'=>function($q){
                    return $q->select('id','model_number');
                },'specification'=>function($q){
                    return $q->select('id','specification');
                }])->select('id','order_num','color_id','product_id','customer_id','model_number_id','specification_id');
            },
                'employee'=>function($q){
                    $q->select('user_id','employee_name','base_pay');
                }
            ])
            ->select('id','order_id','price','employee_user_id')
            ->get()->toArray();
        foreach ($array as $key=>&$val){
            $data[] = [
                'employee_name'=>$val['employee']['employee_name'],
                'order_num'=>$val['order']['order_num'],
                'product_name'=>$val['order']['product']['product_name'],
                'color'=>$val['order']['color']['color'],
                'customer_name'=>$val['order']['customer']['customer_name'],
                'model_number'=>$val['order']['model_number']['model_number'],
                'specification'=>$val['order']['specification']['specification'],
                'base_pay'=>$val['employee']['base_pay'],
                'price'=>$val['price'],
            ];
        }
        return $data;
    }
}
